package com.cong.security.app.social;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.social.connect.ConnectionData;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SocialServiceImpl implements SocialService {

	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	@Override
	public void insert(String key, ConnectionData connectionData) {
		// 将用户数据放入缓存中,有效期十分钟
		redisTemplate.opsForValue().set(key, connectionData, 10, TimeUnit.MINUTES);
		log.info("第三方登陆创建数据信息:[{}]", key);
	}

	@Override
	public ConnectionData select(String key) {
		if (!redisTemplate.hasKey(key)) {
			throw new AppSecretException("无法找到缓存的第三方社交账户数据");
		}
		ConnectionData connectionData = (ConnectionData) redisTemplate.opsForValue().get(key);
		log.info("第三方登陆查询数据信息:[{}]", connectionData);
		return connectionData;
	}
}
